﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Reflection;

namespace UnrealDebugger
{
    // Project Management

    partial class UnrealDebuggerIDE
    {
        private Project _project = null;
        public Project Project { get { return _project; } }

        void SaveProject()
        {
            try
            {
                string projectFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "UnrealDebugger.project");
                _project.SaveToXml(projectFile);
            }
            catch (Exception e)
            {
                ExceptionMessageBox msgBox = new ExceptionMessageBox(e);
                msgBox.ShowDialog();
            }
        }

        void LoadProject()
        {
            string projectFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "UnrealDebugger.project");

            try
            {
                if (File.Exists(projectFile))
                {
                    _project = Project.LoadFromXml(projectFile);
                }
                else
                {
                    Assembly assembly = typeof(UnrealDebuggerIDE).Assembly;
                    Stream projectStream = assembly.GetManifestResourceStream("UnrealDebugger.Resources.Default.project");
                    _project = Project.LoadFromXml(projectStream);
                }
            }
            catch (Exception e)
            {
                ExceptionMessageBox msgBox = new ExceptionMessageBox(e);
                msgBox.ShowDialog();

                _project = new Project();
            }

            _configurationPanel.SetProject(_project);
            _project.Loaded();

            if (_project.CheckForUpdates)
            {
                this._versionChecker = new VersionChecker(this.Title, this.Version);
            }

            BuildRecentFilesList();
        }
    }
}
